package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/region")
public class RegionController {

    //注入用户业务层的方法
    @Autowired
    private IRegionService regionService ;

    /**
     * 跳转后台用户的管理界面
     */
    @RequestMapping("/list.do")
    public String list(){
        //跳转到指定的页面
        return "region/list" ;
    }


    @RequestMapping("/listMenus.do")
    @ResponseBody
    public Object listMenus(Long parentId,String type){
        List<Region> list = regionService.listMenus(parentId);
        //创建一个新的集合去存储加工好的数据
        List<Map> listMap = new ArrayList<>() ;
        //遍历从数据库中查出来的数据
        if("treeview".equals(type)){
            for (Region region : list) {
                listMap.add(region.toTreeView());
            }
            return listMap ;
        }
        return list ;
    }
    @RequestMapping("/saveOrUpdate.do")
    public String saveOrUpdate(Region region){
        //调用业务逻辑方法
        regionService.saveOrUpdate(region);
        return "redirect:/region/list.do" ;
    }

}
